/*
 * UIActions.java
 *
 * Copyright (C) 2008 Paul Nettleship
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
package staticTrailExplorer;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;


/**
 *
 * @author codebauer
 */
 public class UIActions
   {
   static int osmWinNum = 0;
   private JDesktopPane mainPane;
   private UIDialogFile fileDiag = new UIDialogFile();

   /** Creates a new instance of Main */
   public UIActions( JDesktopPane mainPane )
      {
      this.mainPane = mainPane;
      }

   public void setMarker1()
      {
      UIMapWindow mapWin = (UIMapWindow)mainPane.getSelectedFrame();
      mapWin.activeMarker = 0;
      }

   public void setMarker2()
      {
      UIMapWindow mapWin = (UIMapWindow)mainPane.getSelectedFrame();
      mapWin.activeMarker = 1;
      }

   public void changeZoom(int zoomDiff)
      {
      UIMapWindow mapWin = (UIMapWindow)mainPane.getSelectedFrame();
      mapWin.getMap().doZoom(mapWin.getWidth()/2,mapWin.getHeight()/2, zoomDiff);
      repaintAll();
      }

    public void openImageFileMap()
       {
       File phile = fileDiag.openMyFile();
       if (phile != null)
          {
          tileMap map = new tileMap(new mapImage(), null, phile);
          UIMapWindow  mapWin  = new UIMapWindow(phile.getName(), map, this);
          mainPane.add(mapWin);
          mainPane.getDesktopManager().activateFrame(mapWin);
          }
       }

    public void openBinaryFileMap()
       {
       File phile = fileDiag.openMyFile();
       if (phile != null)
          {
          tileMap map = new tileMap(new mapBinary(), new tileCache(this), phile);
          UIMapWindow mapWin = new UIMapWindow(phile.getName(), map, this);
          mainPane.add(mapWin);
          mainPane.getDesktopManager().activateFrame(mapWin);
          }
       }

    public void openAtlasFileMap()
       {
       File phile = fileDiag.openMyFile();
       if (phile != null)
          {
          tileMap map = new tileMap(new mapAtlas(), new tileCache(this), phile);
          UIMapWindow mapWin = new UIMapWindow(phile.getName(), map, this);
          mainPane.add(mapWin);
          mainPane.getDesktopManager().activateFrame(mapWin);
          }
       }

   public void openOsmMap()
      {
      tileMap map = new tileMap(new mapOsm(), new tileCache(this), null);
      UIMapWindow mapWin = new UIMapWindow("OSM(" + osmWinNum++ + ")",map,this);      
      mainPane.add(mapWin);
      mainPane.getDesktopManager().activateFrame(mapWin);
      }

    public void setZoomRange()
       {
       ArrayList nums = new ArrayList();

       while (nums.size() < 2)
          {
          int i = 0;
          Integer current = null;
          nums.clear();
          String s = (String) JOptionPane.showInputDialog("Please enter zoom range e.g (0-14)");
          while (i < s.length())
             {
             char x = s.charAt(i++);

             /* Skip none numbers */
             if ((x < '0') || (x > '9'))
                {
                if (current != null)
                   {
                   nums.add(current);
                   current = null;
                   }
                }

             /* Read decimal */
             if ((x >= '0') && (x <= '9'))
                {
                if (current == null)
                   current = new Integer(0);
                current = (current * 10) + (x - '0');
                }

             /* End of string */
             if (i == s.length())
                {
                if (current != null)
                   nums.add(current);
                }
             }
          }

       /* Debug stuff */
       for (int i = 0; i < nums.size(); i++)
          System.out.print("zoomRange[" + i + "] =" + nums.get(i) + "\n");

       UIMapWindow mapWin = (UIMapWindow) mainPane.getSelectedFrame();
       mapWin.getMap().setZoomRange((Integer)nums.get(0), (Integer)nums.get(1) );
       }

    public void saveAtlasFile()
       {
       setZoomRange();

       try
          {
          UIMapWindow mapWin = (UIMapWindow) mainPane.getSelectedFrame();
          File phile = fileDiag.saveMyFile();
          if (phile != null)
             {
             mapWin.getMap().saveMap( new tileMap(new mapAtlas(), null, phile) );
             }
          }
       catch (IOException ex)
          {
          System.out.print(ex);
          }
       }

    public void saveBinmapFile()
       {
       setZoomRange();

       try
          {
          UIMapWindow mapWin = (UIMapWindow) mainPane.getSelectedFrame();
          File phile = fileDiag.saveMyFile();
          if (phile != null)
             {
             mapWin.getMap().saveMap( new tileMap( new mapBinary(), null, phile) );
             }
          }
       catch (IOException ex)
          {
          System.out.print(ex);
          }
       }

   public void doCalibration()
      {
      UIMapWindow mapWin = (UIMapWindow)mainPane.getSelectedFrame();
      mapDataIF mapData = mapWin.getMap().getMapData();
      mapData.setCalibration();
      repaintAll();
      }

   
   public void closeAll()
      {
      }
   
   public void tileWindows()
      {
      javax.swing.JInternalFrame[] children = mainPane.getAllFrames();
      double width = mainPane.getWidth() / (Math.ceil(Math.sqrt(children.length)));
      double height = mainPane.getHeight() / (Math.ceil(children.length / (Math.ceil(Math.sqrt(children.length)))));
      double posX = 0;
      double posY = 0;
      for (int i = 0; i < children.length; i++)
         {
         children[i].reshape((int) (posX), (int) (posY), (int) (width), (int) (height));
         posX += width;
         if (posX >= mainPane.getWidth())
            {
            posY += height;
            posX = 0;
            }
         }
      }

   void repaintAll()
      {
      mainPane.repaint();
      }

   void moveMap(int x, int y)
      {
      UIMapWindow win = (UIMapWindow) mainPane.getSelectedFrame();
      win.getMap().moveRef( x, y );
      mainPane.repaint();
      }
   }



